﻿using UnityEngine;
using System.Collections;
using MoreMountains.Tools;

namespace MoreMountains.CorgiEngine
{
    //  上挑 的动作
    public class CharacterHoldUpAtk : CharacterAbility
    {
        public override string HelpBoxText() { return "上挑攻击"; }

        public float JumpHeight = 3.5f;
        [Range(0, 1)]
        public float XSpeedSlowDown = 0.4f;

        public DamageInfo DamageInfo = new DamageInfo(35);

        // 从移动平台分离时的时间
        public float MovingPlatformsJumpCollisionOffDuration = 0.05f;

        /// <summary>
        /// Every frame, we check input to see if we should UpAttack
        /// </summary>
        protected override bool CheckHandleInput()
        {
            return _inputManager.NormalAtkButton.State.CurrentState == MMInput.ButtonStates.ButtonDown &&
                _inputManager.PrimaryMovement.y > _inputManager.Threshold.y;
        }
        protected override void HandleInput()
        {
            InitiateDive();
        }

        protected virtual void InitiateDive()
        {
            if (!AbilityPermitted
                || _controller.State.IsGrounded == false // 确保在地面
                || (_movement.CurrentState == CharacterStates.MovementStates.Gripping) // or if it's gripping
                || (_condition.CurrentState != CharacterStates.CharacterConditions.Normal)) // 角色状态正常
            {
                return;
            }
            //  正处于上挑攻击状态时，返回
            if (_movement.CurrentState == CharacterStates.MovementStates.HoldUpAtk)
                return;
            //  开始
            StartCoroutine(HoldUpAtk());
        }

        protected virtual IEnumerator HoldUpAtk()
        {
            //  播放音效
            PlayAbilityStartSfx();
            PlayAbilityUsedSfx();

            // 确保碰撞体开启
            _controller.CollisionsOn();
            //  改变状态
            _movement.ChangeState(CharacterStates.MovementStates.HoldUpAtk);

            //  禁用控制
            ControlInput(false);
            //  停止重力
            _controller.GravityActive(false);
            //  计算伤害
            _character.AttackColliderScript.SetDamage(CalcDamageInfo());


            yield return new WaitForSeconds(0.25f); //  前摇动画等待时间  3/12 秒

            if (_movement.CurrentState == CharacterStates.MovementStates.BeHurt)
                yield break;

            // 如果在移动平台上 ，从移动平台分离
            JumpFromMovingPlatform();

            //  水平速度  维持之前的部分速度
            _controller.SetHorizontalForce(_controller.Speed.x * XSpeedSlowDown);
            //  垂直速度
            _controller.SetVerticalForce(Mathf.Sqrt(2f * JumpHeight * Mathf.Abs(_controller.Parameters.Gravity)));

            //  开启重力 使其上升过程减速
            _controller.GravityActive(true);

            yield return new WaitUntil(() => { return _controller.Speed.y < 1f; });

            //  重置伤害
            _character.AttackColliderScript.Reset();

            if (_movement.CurrentState == CharacterStates.MovementStates.BeHurt)
                yield break;

            //float mustTime = Time.time + 1;
            //Debug.Log("模长 " + _controller.Speed.magnitude);
            //Debug.Log(_controller.Speed.y + "  时刻：" + Time.time);
            //Debug.Log("模长 " + _controller.Speed.magnitude);

            if (_character.ConditionState.CurrentState != CharacterStates.CharacterConditions.Dead)
            {
                // 结束动作音效
                StopAbilityUsedSfx();
                PlayAbilityStopSfx();

                _movement.ChangeState(CharacterStates.MovementStates.Idle);
            }
            //  恢复控制
            ControlInput(true);
        }

        /// <summary>
        /// 从移动的平台中分离开
        /// </summary>
        protected virtual void JumpFromMovingPlatform()
        {
            if (_controller.MovingPlatformMask.Contains(_controller.StandingOn.layer)
                || _controller.MovingOneWayPlatformMask.Contains(_controller.StandingOn.layer))
            {
                // we turn the boxcollider off for a few milliseconds, so the character doesn't get stuck mid air
                StartCoroutine(_controller.DisableCollisionsWithMovingPlatforms(MovingPlatformsJumpCollisionOffDuration));
                _controller.DetachFromMovingPlatform();
            }
        }

        /// <summary>
        /// 初始化动画参数
        /// </summary>
        protected override void InitializeAnimatorParameters()
        {
            RegisterAnimatorParameter("HoldUpAtk", AnimatorControllerParameterType.Bool);
        }

        /// <summary>
        /// 每一帧都调用，改变动画参数
        /// </summary>
        public override void UpdateAnimator()
        {
            MMAnimator.UpdateAnimatorBool(_animator, "HoldUpAtk", (_movement.CurrentState == CharacterStates.MovementStates.HoldUpAtk), _character._animatorParameters);
        }

        /// <summary>
        /// 计算伤害的方法
        /// </summary>
        protected override DamageInfo CalcDamageInfo()
        {
            return DamageInfo;
        }

    }
}
